package com.ljx.blog.article.typehandler;

import cn.hutool.json.JSONUtil;
import com.ljx.blog.article.domain.entity.Article;
import com.ljx.blog.article.domain.vo.resp.ThemeResp;
import com.ljx.blog.article.typehandler.custom.TagList;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-13  12:50
 */
@MappedTypes(TagList.class)
public class ArticleTypeHandler extends BaseTypeHandler<TagList> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, TagList tags, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSONUtil.toJsonStr(tags));
    }

    @Override
    public TagList getNullableResult(ResultSet resultSet, String s) throws SQLException {
        List<String> list = JSONUtil.toList(resultSet.getString(s), String.class);
        return new TagList(list);
    }

    @Override
    public TagList getNullableResult(ResultSet resultSet, int i) throws SQLException {
        List<String> list = JSONUtil.toList(resultSet.getString(i), String.class);
        return new TagList(list);
    }

    @Override
    public TagList getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        List<String> list = JSONUtil.toList(callableStatement.getString(i), String.class);
        return new TagList(list);
    }
}